package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.core.constants.Constants;
import com.example.common.core.utils.ThreadLocalUtil;
import com.example.user.domain.curriculumSchedule.CurriculumSchedule;
import com.example.user.domain.curriculumSchedule.dto.CurriculumScheduleQueryDTO;
import com.example.user.domain.student.Student;
import com.example.user.mapper.CurriculumScheduleMapper;
import com.example.user.mapper.StudentMapper;
import com.example.user.service.ICurriculumScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class CurriculumScheduleServiceImpl extends ServiceImpl<CurriculumScheduleMapper, CurriculumSchedule> implements ICurriculumScheduleService {
    @Autowired
    private CurriculumScheduleMapper curriculumScheduleMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Map<Integer,String[]> teacherGet(CurriculumScheduleQueryDTO curriculumScheduleQueryDTO) {

        String[][] table = new String[7][3];
        String startDate = curriculumScheduleQueryDTO.getStartDate();
        String endDate = curriculumScheduleQueryDTO.getEndDate();
        LambdaQueryWrapper<CurriculumSchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CurriculumSchedule::getClassroomName,CurriculumSchedule::getCourseName,CurriculumSchedule::getDate,CurriculumSchedule::getTime)
                .eq(CurriculumSchedule::getTeacherId,ThreadLocalUtil.get(Constants.USER_ID,Long.class))
                .ge(CurriculumSchedule::getDate, startDate) // 大于等于开始日期
                .le(CurriculumSchedule::getDate, endDate);
        List<CurriculumSchedule> curriculumScheduleList = curriculumScheduleMapper.selectList(queryWrapper);
        for (CurriculumSchedule temp : curriculumScheduleList) {
            String courseName = temp.getCourseName();
            String classroomName = temp.getClassroomName();
            String date = temp.getDate();
            Integer time = temp.getTime();
            int difference = calculateDateDifference(startDate, date);
            table[difference][time] = courseName+"【"+classroomName+"】";
        }
        Map<Integer, String[]> ret = new HashMap<>();
        for (int i = 0; i < table.length; i++) {
            ret.put(i,table[i]);
        }
        return ret;
    }

    @Override
    public Map<Integer, String[]> studentGet(CurriculumScheduleQueryDTO curriculumScheduleQueryDTO) {
        String[][] table = new String[7][3];
        // 根据学生id查询，这个学生所报名的班级
        Long studentId = ThreadLocalUtil.get(Constants.USER_ID,Long.class);
        Student student = studentMapper.selectById(studentId);
        if(student==null || student.getClassroomId()==null){
            return null;
        }
        Long classroomId = student.getClassroomId();
        String startDate = curriculumScheduleQueryDTO.getStartDate();
        String endDate = curriculumScheduleQueryDTO.getEndDate();
        LambdaQueryWrapper<CurriculumSchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CurriculumSchedule::getClassroomName,CurriculumSchedule::getCourseName,CurriculumSchedule::getDate,CurriculumSchedule::getTime)
                .eq(CurriculumSchedule::getClassroomId,classroomId)
                .ge(CurriculumSchedule::getDate, startDate) // 大于等于开始日期
                .le(CurriculumSchedule::getDate, endDate);
        List<CurriculumSchedule> curriculumScheduleList = curriculumScheduleMapper.selectList(queryWrapper);
        for (CurriculumSchedule temp : curriculumScheduleList) {
            String courseName = temp.getCourseName();
            String classroomName = temp.getClassroomName();
            String date = temp.getDate();
            Integer time = temp.getTime();
            int difference = calculateDateDifference(startDate, date);
            table[difference][time] = courseName+"【"+classroomName+"】";
        }
        Map<Integer, String[]> ret = new HashMap<>();
        for (int i = 0; i < table.length; i++) {
            ret.put(i,table[i]);
        }
        return ret;
    }

    public static int calculateDateDifference(String dateStr1, String dateStr2) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将字符串转换为 LocalDate 对象
        LocalDate date1 = LocalDate.parse(dateStr1, formatter);
        LocalDate date2 = LocalDate.parse(dateStr2, formatter);
        // 计算日期之间的天数差
        return (int)ChronoUnit.DAYS.between(date1, date2);
    }
}
